<template>
  <div class="type-nav">
    <div class="container">
      <!-- <div @mouseleave="leaveIndex"> -->
      <div @mouseenter="showSort" @mouseleave="showSort">
        <h2 class="all" v-show="$route.name != 'home' ? true : false">全部商品分类</h2>
        <!-- 过度动画 -->
        <!-- <transition name="sort"> -->
        <div class="sort" v-show="isShow">
          <div class="all-sort-list2">
            <!-- 一级菜单 -->

            <!-- 判断currentIndex是否为当前一级菜单的index, 如果是则加上 class - cur (滑过变背景色) -->
            <!-- <div
              class="item"
              v-for="(c1, index) in categoryList"
              :key="c1.categoryId"
              @mouseenter="changeIndex(index)"
              :class="{ cur: currentIndex == index }"
            > -->
            <div class="item" v-for="c1 in categoryList" :key="c1.categoryId" @click="goSearch">
              <h3>
                <a
                  href="javaScript:;"
                  :data-categoryName="c1.categoryName"
                  :data-category1Id="c1.categoryId"
                >
                  {{ c1.categoryName }}
                </a>
              </h3>

              <!-- 判断currentIndex是否为当前二/三级菜单盒子的index, 如果是则显示 -->
              <!-- <div
                class="item-list clearfix"
                :style="{ display: currentIndex == index ? 'block' : 'none' }"
              > -->
              <div class="item-list clearfix">
                <div class="subitem">
                  <!-- 二级菜单 -->
                  <dl class="fore" v-for="c2 in c1.categoryChild" :key="c2.categoryId">
                    <dt>
                      <a
                        href="javaScript:;"
                        :data-categoryName="c2.categoryName"
                        :data-category2Id="c2.categoryId"
                      >
                        {{ c2.categoryName }}
                      </a>
                    </dt>
                    <dd>
                      <!-- 三级菜单 -->
                      <em v-for="c3 in c2.categoryChild" :key="c3.categoryId">
                        <a
                          href="javaScript:;"
                          :data-categoryName="c3.categoryName"
                          :data-category3Id="c3.categoryId"
                        >
                          {{ c3.categoryName }}
                        </a>
                      </em>
                    </dd>
                  </dl>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- </transition> -->
      </div>
      <nav class="nav" :style="{ marginLeft: $route.name != 'home' ? '0' : '210px' }">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
  import { mapState } from 'vuex';

  // lodash中的节流方法
  // import throttle from 'lodash/throttle';

  // lodash中的防抖方法
  // import debounce from 'lodash/debounce';

  export default {
    name: 'TypeNav',

    mounted() {
      // 组件挂载后, 检测当前路由是否是home, 如果不是isShow改为false, 表示不展示一级菜单
      if (this.$route.name != 'home') {
        this.isShow = false;
      }
    },

    data() {
      return {
        //     // 存储用户鼠标移动到哪一个一类分级的index
        //     currentIndex: -1,
        isShow: true,
      };
    },

    // 定义方法
    methods: {
      // // 鼠标放入一次菜单接收对应菜单的index
      // changeIndex: throttle(function (index) {
      //   // 该lodash方法会返回一个函数, 通过调用函数实现节流效果
      //   this.currentIndex = index;
      // }, 100),

      // // 鼠标移出.item盒子时, 将currentIndex赋值回-1
      // leaveIndex() {
      //   this.currentIndex = -1;
      // },

      // 定义三级联动路由跳转方法
      goSearch(e) {
        let {
          // 结构出数据并起别名
          categoryname: categoryName,
          category1id: category1Id,
          category2id: category2Id,
          category3id: category3Id,
        } = e.target.dataset;

        if (this.$route.query.categoryName === categoryName) return;

        if (categoryName) {
          // 如果点击的是带有数据的标签, 则执行逻辑
          // 创建路由跳转配置
          let location = { name: 'search' };

          // 创建跳转路由时传输的query参数
          let query = { categoryName };

          // 点的是谁就传谁的数据
          if (category1Id) query.category1Id = category1Id;
          else if (category2Id) query.category2Id = category2Id;
          else query.category3Id = category3Id;

          // 收集数据后, 将参数合并到配置项中
          location.query = query;

          // 判断路由中是否有params参数, 如果有并入配置项中
          if (Object.keys(this.$route.params).length) {
            location.params = this.$route.params;
          }

          // 调用push方法跳转路由
          this.$router.push(location);
        }
      },

      // 鼠标滑过全部商品标题展示一级菜单
      showSort() {
        if (this.$route.name != 'home') this.isShow = !this.isShow;
      },
    },

    // 设置计算属性
    computed: {
      // 使用mapSate方法, 将仓库中的属性转成该组件的计算属性
      ...mapState('home', ['categoryList']),
    },
  };
</script>

<style scoped lang="less">
  .type-nav {
    border-bottom: 2px solid #e1251b;

    .container {
      width: 1200px;
      margin: 0 auto;
      display: flex;
      position: relative;

      .all {
        width: 210px;
        height: 45px;
        background-color: #e1251b;
        line-height: 45px;
        text-align: center;
        color: #fff;
        font-size: 14px;
        font-weight: bold;
      }

      .nav {
        a {
          height: 45px;
          margin: 0 22px;
          line-height: 45px;
          font-size: 16px;
          color: #333;
        }
      }

      .sort {
        padding: 5px 0;
        position: absolute;
        left: 0;
        top: 45px;
        width: 210px;
        height: 461px;
        position: absolute;
        background: #fafafa;
        border-top: 2px solid #e1251b;
        z-index: 999;
        .all-sort-list2 {
          .item {
            h3 {
              transition: all 0.3s;
              line-height: 27.3px;
              font-size: 16px;
              font-weight: 400;
              overflow: hidden;
              padding: 0 20px;
              margin: 0;
              a {
                color: #333;
              }
            }

            .item-list {
              display: none;
              position: absolute;
              width: 734px;
              min-height: 460px;
              background: #f7f7f7;
              left: 210px;
              border: 1px solid #ddd;
              top: 0;
              z-index: 9999 !important;

              .subitem {
                float: left;
                width: 650px;
                padding: 0 4px 0 8px;

                dl {
                  border-top: 1px solid #eee;
                  padding: 6px 0;
                  overflow: hidden;
                  zoom: 1;

                  &.fore {
                    border-top: 0;
                  }

                  dt {
                    float: left;
                    width: 54px;
                    line-height: 22px;
                    text-align: right;
                    padding: 3px 6px 0 0;
                    font-weight: 700;
                  }

                  dd {
                    float: left;
                    width: 415px;
                    padding: 3px 0 0;
                    overflow: hidden;

                    em {
                      float: left;
                      height: 14px;
                      line-height: 14px;
                      padding: 0 8px;
                      margin-top: 5px;
                      border-left: 1px solid #ccc;
                    }
                  }
                }
              }
            }
            &:hover .item-list {
              display: block;
            }
            &:hover h3 {
              background-color: skyblue;
            }
          }

          // 鼠标滑过一级菜单变色
          // .cur {
          //   background-color: skyblue;
          // }
        }
      }

      /* 过渡动画的样式 */
      // 过度动画开始 (进入)
      .sort-enter {
        height: 0px;
        // opacity: 0;
      }
      // 过渡动画结束 (进入)
      .sort-enter-to {
        height: 460px;
        // opacity: 1;
      }
      // 过度动画时间, 速率
      .sort-enter-active {
        transition: all 0.2s linear;
        overflow: hidden;
      }
    }
  }
</style>
